home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 22
/
Cream of the Crop 22.iso
/
os2
/
splot170.zip
/
demo
/
fftfilt.spt
< prev
next >
Wrap
Text File
|
1996-10-10
|
3KB
|
148 lines
#include <splot.h>
double *dat;
double *a;
double yint,slope;
int i,j,k,n,n2;
char tmp[80];
// datafile name
char *datname = "learn.dat";
// filter parameters in hertz
int cutoff = 75;
int rolloff = 25;
int notch = -20;
int width = 10;
// data scaling factors
double lpos_scale = 0.234; // to um
double time_scale = 0.408; // to ms
main()
{
readdata(datname,dat);
j = arraydim(dat,0);
// make n next largest power of 2
n = 1;
while (n < j) n <<= 1;
n2 = n * n;
// temp arrays
double freq[n][3];
double filt[n][2];
double lpos[n][2];
double pow[n/2 +1][2];
// copy lens positions and scale
for (i = 0; i < j;i++)
{
lpos[i][0] = time_scale * i;
lpos[i][1] = dat[i][3] * lpos_scale;
}
// pad end of position array
for (i = j; i < n;i++)
{
lpos[i][0] = time_scale * i;
lpos[i][1] = dat[j-1][3] * lpos_scale;
}
// subtract trend
set(XRANGE,80,280);
fitline(lpos,0,1,&yint,&slope);
set(XRANGE,-1e6,1e6);
for (i = 0; i < n;i++)
{
lpos[i][1] = lpos[i][1] - (yint + i * slope);
freq[i][0] = (double) i / n / time_scale * 1000;
freq[i][1] = lpos[i][1]; // real part
freq[i][2] = 0; // complex part
}
// transform
fft(freq,1,2);
//plotdata(freq,0,1);
//fft(freq,1,2,-1);
/* build filter */
int icut,iroll,inotch,iwidth;
/* convert filt params to integer */
icut = (int) (cutoff * n * time_scale / 1000);
iroll = (int) (rolloff * n * time_scale / 1000);
inotch = (int) (notch * n * time_scale / 1000);
iwidth = (int) (width * n * time_scale / 1000);
for (i = 1; i < n / 2;i++)
{
filt[i][0] = (double) i / n / time_scale * 1000;
if (i < icut)
{
filt[i][1] = 1.0;
filt[n-i][1] = 1.0;
}
else if (i < icut + iroll)
{
filt[i][1] = 1.0 - (double) (i - icut) / iroll;
filt[n-i][1] = 1.0 - (double) (i - icut) / iroll;
}
else
{
filt[i][1] = 0;
filt[n-i][1] = 0;
}
if (i >= inotch - iwidth && i <= inotch + iwidth)
{
filt[i][1] = fabs((double) (i - inotch) / iwidth);
filt[n-i][1] = fabs((double) (i - inotch) / iwidth);
}
}
// filter data
for (i = 1; i < n;i++)
{
freq[i][1] *= filt[i][1];
freq[i][2] *= filt[i][1];
}
// transform back
fft(freq,1,2,-1);
for (i = 1; i < n;i++)
{
freq[i][0] = lpos[i][0];
}
// plot results
abox();
ascale(XAXES,0,300);
set(AXESCLIP,ON);
#if 1
ascale(YAXES,-300,100);
plotdata(lpos,0,1,freq,0,1);
label(BOTTOM,"Time (ms)");
label(LEFT,"Lens position (!m!m)");
text( 8.41,11.91,"Extreme Kink");
sprintf(tmp,"filter cutoff %d Hz", cutoff);
text(tmp);
#else
for (i = 1; i < n;i++)
{
lpos[i][1] = lpos[i][1] - freq[i][1];
}
plotdata(lpos,0,1);
label(BOTTOM,"Time (ms)");
label(LEFT,"Delta Lens position (!m!m)");
text( 8.41,18.59,"Extreme Kink");
sprintf(tmp,"filter cutoff %d Hz", cutoff);
text(tmp);
#endif
label(TOP,"TH55 drum spin 100");
set(FONTMULT,0.5);
text(0.5,0.5,CURFILENAME);
}